package com.kasa.gps;

import java.io.UnsupportedEncodingException;
import java.util.Set;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.database.Cursor;
import android.graphics.Color;
import android.os.Bundle;
import android.text.Editable;
import android.text.InputFilter;
import android.text.TextWatcher;
import android.text.TextUtils.TruncateAt;
import android.util.TypedValue;
import android.view.Gravity;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup.LayoutParams;
import android.widget.ArrayAdapter;
import android.widget.AutoCompleteTextView;
import android.widget.Button;
import android.widget.DatePicker;
import android.widget.EditText;
import android.widget.HorizontalScrollView;
import android.widget.LinearLayout;
import android.widget.ScrollView;
import android.widget.Spinner;
import android.widget.TableLayout;
import android.widget.TableRow;
import android.widget.TextView;
import android.widget.TimePicker;

import com.kasa.gps.db.OrderGPSDBAdapter;
import com.kasa.gps.db.table.VehiclesInfoTableAdapter;
import com.kasa.gps.service.TrackService;
import com.kasa.gps.service.TrackService.TrackServiceHandler;
import com.kasa.gps.tool.Tool;
import com.kasa.gps.view.ValueButton;

public class SettingActivity extends Activity {

	private TableLayout settingLayout = null;
	
	public static final String SETTING_OP_KEY = "setting_op_key";
	public byte setting_op_type = OrderGPSActivity.ITEMID_NEW_SETTING;
	private String licensePlates = null;
	private String transportPlans = null;
	private Object[]licensePlatesArray = null;
	

	private AutoCompleteTextView licensePlates_edit = null;
	private AutoCompleteTextView transportPlans_edit = null;
	private ValueButton expectedTime_edit = null;
	private ValueButton locationUpdateFrequency_edit = null;
	private ValueButton locationUpdateDistance_edit = null;
	private ValueButton dataUploadFrequency_edit = null;
	private ValueButton uploadServer_edit = null;
	
	//private AutoCompleteTextView uploadServer_edit = null;
	
	private Button save = null;
	
	private VehiclesInfoTableAdapter vehiclesInfoTableAdapter = null;
	
	//private boolean isServiceRunning = false;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		ScrollView verticalScrollView = new ScrollView(this);
		HorizontalScrollView horizontalScrollView = new HorizontalScrollView(this);
		verticalScrollView.addView(horizontalScrollView);
		settingLayout = new TableLayout(this);
		settingLayout.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT,LayoutParams.FILL_PARENT));
		horizontalScrollView.addView(settingLayout);
		Intent intent = getIntent();
		if(intent!=null){
			setting_op_type = intent.getByteExtra(SETTING_OP_KEY, setting_op_type);
			if(setting_op_type == OrderGPSActivity.ITEMID_UPDATE_SETTING){
				licensePlates = intent.getStringExtra(VehiclesInfoTableAdapter.LICENSE_PLATES);
				transportPlans = intent.getStringExtra(VehiclesInfoTableAdapter.TRANSPORT_PLANS);
			}else{
				licensePlatesArray = (Object[])intent.getSerializableExtra(VehiclesInfoTableAdapter.LICENSE_PLATES);
			}
			
		}
		
		//isServiceRunning = Tool.isServiceRunning(this, TrackService.class.getName());
		
		setLayout();
		setContentView(verticalScrollView);
		setListener();
	}
	
	private void setLayout(){
		//车牌
		TableRow licensePlates_row = new TableRow(this);
		licensePlates_row.setGravity(Gravity.CENTER);
		settingLayout.addView(licensePlates_row);
		//
		TextView licensePlates_title = new TextView(this);
		licensePlates_title.setTextSize(TypedValue.COMPLEX_UNIT_SP, Tool.TEXT_SIZE);
		licensePlates_title.setText(R.string.licensePlates_title);
		licensePlates_row.addView(licensePlates_title);
		//
		licensePlates_edit = new AutoCompleteTextView(this);
		licensePlates_edit.setTextSize(TypedValue.COMPLEX_UNIT_SP, Tool.TEXT_SIZE);
		licensePlates_edit.setSingleLine();
		licensePlates_row.addView(licensePlates_edit);
		//运输计划
		TableRow transportPlans_row = new TableRow(this);
		transportPlans_row.setGravity(Gravity.CENTER);
		settingLayout.addView(transportPlans_row);
		//
		TextView transportPlans_title = new TextView(this);
		transportPlans_title.setTextSize(TypedValue.COMPLEX_UNIT_SP, Tool.TEXT_SIZE);
		transportPlans_title.setText(R.string.transportplans_title);
		transportPlans_row.addView(transportPlans_title);
		//
		transportPlans_edit = new AutoCompleteTextView(this);
		transportPlans_edit.setTextSize(TypedValue.COMPLEX_UNIT_SP, Tool.TEXT_SIZE);
		transportPlans_edit.setSingleLine();
		transportPlans_row.addView(transportPlans_edit);
		
		//预期计划完成时间
		TableRow expectedTime_row = new TableRow(this);
		expectedTime_row.setGravity(Gravity.CENTER);
		settingLayout.addView(expectedTime_row);
		//
		TextView expectedTime_title = new TextView(this);
		expectedTime_title.setTextSize(TypedValue.COMPLEX_UNIT_SP, Tool.TEXT_SIZE);
		expectedTime_title.setText(R.string.expectedtime_title);
		expectedTime_row.addView(expectedTime_title);
		//
		expectedTime_edit = new ValueButton(this);
		expectedTime_row.addView(expectedTime_edit);
		
		//位置更新频率
		TableRow locationUpdateFrequency_row = new TableRow(this);
		locationUpdateFrequency_row.setGravity(Gravity.CENTER);
		settingLayout.addView(locationUpdateFrequency_row);
		//
		TextView locationUpdateFrequency_title = new TextView(this);
		locationUpdateFrequency_title.setTextSize(TypedValue.COMPLEX_UNIT_SP, Tool.TEXT_SIZE);
		locationUpdateFrequency_title.setText(R.string.locationUpdateFrequency_title);
		locationUpdateFrequency_row.addView(locationUpdateFrequency_title);
		//
		locationUpdateFrequency_edit = new ValueButton(this);
		locationUpdateFrequency_edit.setTextSize(TypedValue.COMPLEX_UNIT_SP, Tool.TEXT_SIZE);
		locationUpdateFrequency_row.addView(locationUpdateFrequency_edit);
		
		//位置更新间距
		TableRow locationUpdateDistance_row = new TableRow(this);
		locationUpdateDistance_row.setGravity(Gravity.CENTER);
		settingLayout.addView(locationUpdateDistance_row);
		//
		TextView locationUpdateDistance_title = new TextView(this);
		locationUpdateDistance_title.setTextSize(TypedValue.COMPLEX_UNIT_SP, Tool.TEXT_SIZE);
		locationUpdateDistance_title.setText(R.string.locationUpdateDistance_title);
		locationUpdateDistance_row.addView(locationUpdateDistance_title);
		//
		locationUpdateDistance_edit = new ValueButton(this);
		locationUpdateDistance_edit.setTextSize(TypedValue.COMPLEX_UNIT_SP, Tool.TEXT_SIZE);
		locationUpdateDistance_row.addView(locationUpdateDistance_edit);
		
		
		//上传服务器
		TableRow uploadServer_row = new TableRow(this);
		uploadServer_row.setGravity(Gravity.CENTER);
		settingLayout.addView(uploadServer_row);
		//
		TextView uploadServer_title = new TextView(this);
		uploadServer_title.setTextSize(TypedValue.COMPLEX_UNIT_SP, Tool.TEXT_SIZE);
		uploadServer_title.setText(R.string.uploadServer_title);
		uploadServer_row.addView(uploadServer_title);
		//
		uploadServer_edit = new ValueButton(this);
		uploadServer_edit.setTextSize(TypedValue.COMPLEX_UNIT_SP, Tool.TEXT_SIZE);
		uploadServer_edit.setEllipsize(TruncateAt.MARQUEE);
		uploadServer_edit.setMarqueeRepeatLimit(1000);
		uploadServer_row.addView(uploadServer_edit);
		
		//数据上传频率
		TableRow dataUploadFrequency_row = new TableRow(this);
		dataUploadFrequency_row.setGravity(Gravity.CENTER);
		settingLayout.addView(dataUploadFrequency_row);
		//
		TextView dataUploadFrequency_title = new TextView(this);
		dataUploadFrequency_title.setTextSize(TypedValue.COMPLEX_UNIT_SP, Tool.TEXT_SIZE);
		dataUploadFrequency_title.setText(R.string.dataUploadFrequency_title);
		dataUploadFrequency_row.addView(dataUploadFrequency_title);
		//
		dataUploadFrequency_edit = new ValueButton(this);
		dataUploadFrequency_row.addView(dataUploadFrequency_edit);
		
		//
		TableRow op_row = new TableRow(this);
		op_row.setGravity(Gravity.CENTER);
		settingLayout.addView(op_row);
		//
		Button backtoMain = new Button(this);
		backtoMain.setTextSize(TypedValue.COMPLEX_UNIT_SP, Tool.TEXT_SIZE);
		backtoMain.setText(R.string.backtomain_title);
		op_row.addView(backtoMain);
		//
		save = new Button(this);
		save.setTextSize(TypedValue.COMPLEX_UNIT_SP, Tool.TEXT_SIZE);
		save.setText(R.string.save_title);
		save.setEnabled(false);
		op_row.addView(save);

		vehiclesInfoTableAdapter = new VehiclesInfoTableAdapter(SettingActivity.this);
		//如果是更新操作，则车牌不能修改
		if(setting_op_type == OrderGPSActivity.ITEMID_UPDATE_SETTING){
			licensePlates_edit.setText(licensePlates);
			licensePlates_edit.setEnabled(false);
			//
			transportPlans_edit.setText(transportPlans);
			transportPlans_edit.setEnabled(false);
			//
			Cursor cursor = null;
			try {
				cursor = vehiclesInfoTableAdapter.getVehiclesInfo(new String(licensePlates.getBytes(OrderGPSDBAdapter.ENCODING),OrderGPSDBAdapter.ENCODING),new String(transportPlans.getBytes(OrderGPSDBAdapter.ENCODING),OrderGPSDBAdapter.ENCODING));
			} catch (UnsupportedEncodingException e) {
				e.printStackTrace();
			}
			if(cursor.getCount() >0){
				Tool tool = new Tool(SettingActivity.this);
				cursor.moveToFirst();
				
				expectedTime_edit.setValue(cursor.getString(cursor.getColumnIndexOrThrow(VehiclesInfoTableAdapter.EXPECTED_TIME)));
				expectedTime_edit.setText(expectedTime_edit.getValue());
				
				locationUpdateFrequency_edit.setValue(cursor.getString(cursor.getColumnIndexOrThrow(VehiclesInfoTableAdapter.LOCATION_UPDATE_FREQUENCY)));
				locationUpdateFrequency_edit.setText(tool.convert(locationUpdateFrequency_edit.getValue(),tool.UNITS_TIME));
				
				locationUpdateDistance_edit.setValue(cursor.getString(cursor.getColumnIndexOrThrow(VehiclesInfoTableAdapter.LOCATION_UPDATE_DISTANCE)));
				locationUpdateDistance_edit.setText(tool.convert(locationUpdateDistance_edit.getValue(),tool.UNITS_DISTANCE));
				
				uploadServer_edit.setValue(cursor.getString(cursor.getColumnIndexOrThrow(VehiclesInfoTableAdapter.UPLOAD_SERVER)));
				uploadServer_edit.setText(uploadServer_edit.getValue());
				
				dataUploadFrequency_edit.setValue(cursor.getString(cursor.getColumnIndexOrThrow(VehiclesInfoTableAdapter.DATA_UPLOAD_FREQUENCY)));
				dataUploadFrequency_edit.setText(tool.convert(dataUploadFrequency_edit.getValue(),tool.UNITS_TIME));
				
				cursor.close();
				vehiclesInfoTableAdapter.close();
			}
		}else{
			//如果是新建，则提示填写
			licensePlates_edit.setHint(R.string.licensePlates_edit_hint);
			transportPlans_edit.setHint(R.string.transportPlans_edit_hint);
			uploadServer_edit.setHint(R.string.uploadServer_edit_hint);
			if(licensePlatesArray.length >0){
				ArrayAdapter<Object> licensePlatesAdapter = new ArrayAdapter<Object>(SettingActivity.this, android.R.layout.simple_dropdown_item_1line,licensePlatesArray);
				licensePlates_edit.setAdapter(licensePlatesAdapter);
			}
			
			expectedTime_edit.setText(R.string.expectedTime_hint);
			expectedTime_edit.setTextColor(Color.GRAY);
			
			locationUpdateFrequency_edit.setValue("4-1");
			locationUpdateFrequency_edit.setText("5分");
			
			locationUpdateDistance_edit.setValue("0-1");
			locationUpdateDistance_edit.setText("1x100米");
			
			dataUploadFrequency_edit.setValue("9-1");
			dataUploadFrequency_edit.setText("10分");
		}
		
		
		backtoMain.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				Tool.backToMain(SettingActivity.this);
			}
		});
		
		save.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				try{
					if(setting_op_type == OrderGPSActivity.ITEMID_UPDATE_SETTING){
						
						if(vehiclesInfoTableAdapter.updateVehiclesInfo(new String(licensePlates.getBytes(OrderGPSDBAdapter.ENCODING),OrderGPSDBAdapter.ENCODING), new String(transportPlans.getBytes(OrderGPSDBAdapter.ENCODING),OrderGPSDBAdapter.ENCODING),expectedTime_edit.getText().toString(),locationUpdateFrequency_edit.getValue(),locationUpdateDistance_edit.getValue(), uploadServer_edit.getText().toString(), dataUploadFrequency_edit.getValue())){
							Tool.showMessage(SettingActivity.this, getString(R.string.save_update_setting_successful));
							save.setEnabled(false);
							
							//发送消息到后台服务，通知修改
							TrackService.TrackServiceHandler handler = new TrackServiceHandler();
							if(expectedTime_edit.isValueChange()){
								handler.sendEmptyMessage(handler.WHAT_EXPECTED_TIME_CHANGE_2);
							}
							if(locationUpdateFrequency_edit.isValueChange()|| locationUpdateDistance_edit.isValueChange()){
								handler.sendEmptyMessage(handler.WHAT_LOCATON_UPDATE_CHANGE_3);
							}
							if(uploadServer_edit.isValueChange() || dataUploadFrequency_edit.isValueChange()){
								handler.sendEmptyMessage(handler.WHAT_DATA_UPLOAD_CHANGE_4);
							}
						}else{
							Tool.showMessage(SettingActivity.this, getString(R.string.save_update_setting_unsuccessful));
						}
						
					}else{
						
						if(vehiclesInfoTableAdapter.insertVehiclesInfo(new String(licensePlates_edit.getText().toString().getBytes(OrderGPSDBAdapter.ENCODING),OrderGPSDBAdapter.ENCODING), new String(transportPlans_edit.getText().toString().getBytes(OrderGPSDBAdapter.ENCODING),OrderGPSDBAdapter.ENCODING),expectedTime_edit.getText().toString(),locationUpdateFrequency_edit.getValue(),locationUpdateDistance_edit.getValue(), uploadServer_edit.getText().toString(), dataUploadFrequency_edit.getValue())){
							Tool.showMessage(SettingActivity.this, getString(R.string.save_new_setting_successful));
							save.setEnabled(false);
						}else{
							Tool.showMessage(SettingActivity.this, getString(R.string.save_new_setting_unsuccessful));
						}
						
					}
					vehiclesInfoTableAdapter.close();
				}catch(UnsupportedEncodingException e){
					
				}
				
				
			}
		});
	}

	
	private void setListener(){
		
		this.expectedTime_edit.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				LinearLayout expectedTime_layout = new LinearLayout(SettingActivity.this);
				expectedTime_layout.setOrientation(LinearLayout.VERTICAL);
				//日期
				final DatePicker datePicker = new DatePicker(SettingActivity.this);
				expectedTime_layout.addView(datePicker);
				//时间
				final TimePicker timePicker = new TimePicker(SettingActivity.this);
				timePicker.setIs24HourView(true);
				expectedTime_layout.addView(timePicker);
				
				if(setting_op_type == OrderGPSActivity.ITEMID_UPDATE_SETTING){
					String[] dateTime = expectedTime_edit.getText().toString().split(Tool.SPLIT_DATE_TIME);
					String[] date = dateTime[0].split(Tool.SPLIT_Y_M_D);
					String[] time = dateTime[1].split(Tool.SPLIT_H_M);
					
					datePicker.updateDate(Integer.parseInt(date[0]), Integer.parseInt(date[1])-1, Integer.parseInt(date[2]));
					timePicker.setCurrentHour(Integer.parseInt(time[0]));
					timePicker.setCurrentMinute(Integer.parseInt(time[1]));
				}
				
				AlertDialog.Builder builder = new AlertDialog.Builder(SettingActivity.this);
				builder.setTitle(R.string.expectedTime_edit_dialog_title)
						.setView(expectedTime_layout)
						.setPositiveButton(R.string.dialog_positivebutton_text, new DialogInterface.OnClickListener() {
							
							@Override
							public void onClick(DialogInterface dialog, int which) {
								StringBuffer str = new StringBuffer();
								str.append(datePicker.getYear()).append(Tool.SPLIT_Y_M_D);
								if(datePicker.getMonth()+1 < 10){
									str.append("0");
								}
								str.append(datePicker.getMonth()+1).append(Tool.SPLIT_Y_M_D);
								if(datePicker.getDayOfMonth() < 10){
									str.append("0");
								}
								str.append(datePicker.getDayOfMonth()).append(Tool.SPLIT_DATE_TIME);
								if(timePicker.getCurrentHour()<10){
									str.append("0");
								}
								str.append(timePicker.getCurrentHour()).append(Tool.SPLIT_H_M);
								if(timePicker.getCurrentMinute()<10){
									str.append("0");
								}
								str.append(timePicker.getCurrentMinute());
								expectedTime_edit.setValue(str.toString());
								expectedTime_edit.setText(expectedTime_edit.getValue());
								expectedTime_edit.setTextColor(Color.BLACK);
								expectedTime_edit.setValueChange(true);
								save.setEnabled(true);
							}
						})
						.setNegativeButton(R.string.dialog_negativebutton_text, null)
						.show();
				
			}
		});
		
		this.locationUpdateFrequency_edit.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				genFrequencyDialog(locationUpdateFrequency_edit,R.array.frequency_list,R.array.timeUnits_list,R.string.frequency_edit_dialog_title);
			}
		});
		
		this.locationUpdateDistance_edit.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				genFrequencyDialog(locationUpdateDistance_edit,R.array.frequency_list,R.array.distanceUnits_list,R.string.distance_edit_dialog_title);
			}
		});
		
		this.uploadServer_edit.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				
				LinearLayout uploadServer_layout = new LinearLayout(SettingActivity.this);
				uploadServer_layout.setOrientation(LinearLayout.VERTICAL);
				uploadServer_layout.setGravity(Gravity.CENTER);
				//服务器地址
				TextView uploadServer_addr_title = new TextView(SettingActivity.this);
				uploadServer_addr_title.setText(R.string.uploadServer_addr_title);
				uploadServer_layout.addView(uploadServer_addr_title);
				
				//
				final AutoCompleteTextView uploadServer_addr_edit = new AutoCompleteTextView(SettingActivity.this);
				uploadServer_addr_edit.setSingleLine();
				uploadServer_layout.addView(uploadServer_addr_edit);
				Set<String> uploadServer = vehiclesInfoTableAdapter.getAllUploadServer();
				if(uploadServer.size()>0){
					ArrayAdapter<Object> uploadServerAdapter = new ArrayAdapter<Object>(SettingActivity.this, android.R.layout.simple_dropdown_item_1line,uploadServer.toArray());
					uploadServer_addr_edit.setAdapter(uploadServerAdapter);
				}
				
				//端口
				TextView uploadServer_port_title = new TextView(SettingActivity.this);
				uploadServer_port_title.setText(R.string.uploadServer_port_title);
				uploadServer_layout.addView(uploadServer_port_title);
				//
				final EditText uploadServer_port_edit = new EditText(SettingActivity.this);
				uploadServer_port_edit.setSingleLine();
				uploadServer_layout.addView(uploadServer_port_edit);
				uploadServer_port_edit.setFilters(new InputFilter[]{new InputFilter.LengthFilter(5)});
				//对输入监听
				uploadServer_port_edit.addTextChangedListener(new TextWatcher(){
					
					int start  = 0;
					int length = 0;
					
					@Override
					public void afterTextChanged(Editable s) {
						if(s.length() > length){
							char c = s.charAt(start);
							if(c!='1' && c!='2'&& c!='3'&& c!='4'&& c!='5'&& c!='6'&& c!='7'&& c!='8'&& c!='9'&& c!='0'){
								s.delete(start, start+1);
							}
						}
					}

					@Override
					public void beforeTextChanged(CharSequence s, int start,
							int count, int after) {
						this.start = start;
						this.length = s.length();
					}

					@Override
					public void onTextChanged(CharSequence s, int start,
							int before, int count) {
						
					}});
						
				if(setting_op_type == OrderGPSActivity.ITEMID_UPDATE_SETTING){
					String server = SettingActivity.this.uploadServer_edit.getValue();
					if(!server.trim().equals("")){
						String []f = server.split((Tool.SPLIT_ADDR_PORT));
						if(f.length > 1){
							uploadServer_addr_edit.setText(f[0]);
							uploadServer_port_edit.setText(f[1]);
						}
					}					
				}
				
				AlertDialog.Builder builder = new AlertDialog.Builder(SettingActivity.this);
				builder.setTitle(R.string.uploadServer_dialog_title)
				       .setView(uploadServer_layout)
				       .setPositiveButton(R.string.dialog_positivebutton_text, new DialogInterface.OnClickListener() {
						
						@Override
						public void onClick(DialogInterface dialog, int which) {
							uploadServer_edit.setText(uploadServer_addr_edit.getText().toString() +Tool.SPLIT_ADDR_PORT +uploadServer_port_edit.getText().toString());
							uploadServer_edit.setValue(uploadServer_edit.getText().toString());
							uploadServer_edit.setTextColor(Color.BLACK);
							uploadServer_edit.setValueChange(true);
							save.setEnabled(true);
						}
					})
				       .setNegativeButton(R.string.dialog_negativebutton_text, null)
				       .show();
				
			}
		});
		
		this.dataUploadFrequency_edit.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				genFrequencyDialog(dataUploadFrequency_edit,R.array.frequency_list,R.array.timeUnits_list,R.string.frequency_edit_dialog_title);
				
			}
		});
		
		
	}
	
	private void genFrequencyDialog(final ValueButton button,int value_textArrayResId,int units_textArrayResId,int dialog_titleId){

		
		LinearLayout frequency_layout = new LinearLayout(SettingActivity.this);
		frequency_layout.setOrientation(LinearLayout.HORIZONTAL);
		frequency_layout.setGravity(Gravity.CENTER);
		//时间列表
		final Spinner frequency_list = new Spinner(SettingActivity.this);
		ArrayAdapter<CharSequence> locationUpdateFrequency_adapter = ArrayAdapter.createFromResource(SettingActivity.this, value_textArrayResId,android.R.layout.simple_spinner_item);
		locationUpdateFrequency_adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
		frequency_list.setAdapter(locationUpdateFrequency_adapter);
		frequency_layout.addView(frequency_list);
		//时间单位
		final Spinner timeUnit_list = new Spinner(SettingActivity.this);
		ArrayAdapter<CharSequence> timeUnit_adapter = ArrayAdapter.createFromResource(SettingActivity.this, units_textArrayResId,android.R.layout.simple_spinner_item);
		timeUnit_adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
		timeUnit_list.setAdapter(timeUnit_adapter);
		frequency_layout.addView(timeUnit_list);
				
		if(setting_op_type == OrderGPSActivity.ITEMID_UPDATE_SETTING){
			String []f = button.getValue().split(Tool.SPLIT);
			frequency_list.setSelection(Byte.parseByte(f[0]));
			timeUnit_list.setSelection(Byte.parseByte(f[1]));
		}
		
		AlertDialog.Builder builder = new AlertDialog.Builder(SettingActivity.this);
		builder.setTitle(dialog_titleId)
		       .setView(frequency_layout)
		       .setPositiveButton(R.string.dialog_positivebutton_text, new DialogInterface.OnClickListener() {
				
				@Override
				public void onClick(DialogInterface dialog, int which) {
					button.setText(frequency_list.getSelectedItem().toString() + timeUnit_list.getSelectedItem().toString());
					button.setValue(frequency_list.getSelectedItemId()+Tool.SPLIT+timeUnit_list.getSelectedItemId());
					button.setTextColor(Color.BLACK);
					button.setValueChange(true);
					save.setEnabled(true);
				}
			})
		       .setNegativeButton(R.string.dialog_negativebutton_text, null)
		       .show();
		
	
	}
	
	@Override
	protected void onDestroy() {
		vehiclesInfoTableAdapter = null;
		super.onDestroy();
	}
	
	/**重新“返回键”的事件*/
	@Override
	public void onBackPressed() {
		Tool.backToMain(SettingActivity.this);
	}
	
//	@Override
//	public boolean onKeyDown(int keyCode, KeyEvent event) {
//		
//		
//		if(keyCode == KeyEvent.KEYCODE_BACK){
//			Intent main = new Intent(SettingActivity.this, OrderGPSActivity.class);
//			//main.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
//			startActivity(main);
//		}
//		
//		return false;
//	}
}
